// @import
import HeroCtrl, { g_oHeroCtrlMap } from "../../ctrl/HeroCtrl";
import HeroCtrlFactory from "../../ctrl/HeroCtrlFactory";
import SceneData from "../../data/SceneData";
import UserData from "../../data/UserData";
import { g_oCmdHandlerMap } from "../ICmdHandler";

// @import
import mod_gameMsgProtocol = require("../../msg/mod_gameMsgProtocol");

/**
 * 用户入场结果处理器
 */
export default class UserEntryResultHandler {
    /**
     * 处理结果
     * 
     * @param oResult 结果
     */
    handle(oResult: mod_gameMsgProtocol.msg.UserEntryResult): void {
        if (null == oResult || 
            oResult.userId == UserData.getMyData()._nUserId) {
            return;
        }

        if ("GameScene" != SceneData._strCurrSceneName) {
            // 如果还没有进入游戏场景,
            // 则直接退出!
            // 不处理用户进入结果
            console.log("当前场景 = " + SceneData._strCurrSceneName);
            return;
        }

        HeroCtrlFactory.createByAvatar(oResult.heroAvatar, (oNewHeroNode) => {
            let oHeroCtrl: HeroCtrl = oNewHeroNode.getComponent(HeroCtrl);
            oHeroCtrl._nUserId = oResult.userId;
            oHeroCtrl._strUserName = oResult.userName;
            oHeroCtrl._nMaxHP = 1;
            oHeroCtrl._nCurrHP = 1;

            g_oHeroCtrlMap.set(oResult.userId, oHeroCtrl);

            cc.find("/Canvas/Holder_HeroGroup").addChild(oNewHeroNode);
        });
    }
}

// @autoExec
(function() {
    console.log("注册 -- UserEntryResultHandler");
    g_oCmdHandlerMap.set(
        mod_gameMsgProtocol.msg.MsgCode.USER_ENTRY_RESULT, 
        new UserEntryResultHandler()
    );
})();
